
******************************************************************************;

/* 				This file produces the results of Figure 1					*/
/*			The macro generates the numbers plotted in the figure			*/

******************************************************************************;

libname rep "C:\Users\~";

data filtered; set rep.mydata; run;

option missing = " ";
%macro print;
data N; set N; Estimate = nValue1; if Label1 = "Used"; run;
data Nobs; set N; Parameter = "n"; keep EstType Parameter Estimate; run;
data r2; set fit; Parameter = "r2"; keep EstType Parameter RSquare; rename RSquare = Estimate;  run;
data final; set parest Nobs r2 ; drop EstType StdErr Probt DF; run;

data est; set final; keep parameter estimate; run;
data est; set est; rename Estimate = syy_&j;
Type = "rho"; run;

data tval; set final; keep parameter tValue; run;
data tval; set tval; rename tValue = syy_&j; 
Type = "t"; run;
data app; set est tval; run;
data final&j; retain type Parameter syy_&j; set app; 
if Parameter not in ("a1") then delete; 
drop Parameter; run;
proc sort data = final&j; by Type; run;
proc print data = final&j; run;
%mend;


%macro decomp;

%do k=1 %to 4;
%if &k =1 %then %do; %let xvar = hfflow_low; %let fvar1 = hfflow_high; %let fvar2= active_low; %let fvar3= active_high; %end;
%if &k =2 %then %do; %let xvar = hfflow_high; %let fvar1 = hfflow_low; %let fvar2= active_low; %let fvar3= active_high; %end;
%if &k =3 %then %do; %let xvar = active_low; %let fvar1 = hfflow_low; %let fvar2= hfflow_high; %let fvar3= active_high; %end; 
%if &k =4 %then %do; %let xvar = active_high; %let fvar1 = hfflow_low; %let fvar2= hfflow_high; %let fvar3= active_low; %end;

%do j=1 %to 12;

proc sql;
create table ltret as select distinct a.*, exp(sum(log(1+b.syy)))-1 as ltret
from filtered as a, filtered as b 
where 0< intck('month', a.date, b.date) <=&j group by a.date having count(b.syy) = &j
order by date; quit;

proc reg data = ltret plot = none outest = beta;
model syy = &xvar &fvar1 &fvar2 &fvar3 mktrf amihud tover hml smb; 
output out = rtemp r = yres; run; quit;

proc sql;
create table rtemp2 as select a.*, b.&xvar as beta
from rtemp as a, beta as b; quit;

data rfinal; set rtemp2; yhat = &xvar*beta; run;

proc standard data = rfinal mean=0 std =1 out = zvars;
var ltret syy &fvar1 &fvar2 &fvar3 mktrf1 amihud1 tover1 hml1 smb1 yres yhat; run;

%let eq = f1*&fvar1 + f2*&fvar2 + f3*&fvar3;
ods output "Nonlinear GMM Summary of Residual Errors" = fit;
ods output "Nonlinear GMM Parameter Estimates" = parest;
ods output "Estimation Summary Statistics" = N;
proc model data=zvars plot =none;
endo ltret ; exog &fvar1 &fvar2 &fvar3 yhat yres mktrf1 amihud1 tover1 hml1 smb1 ; instruments _exog_;
ltret =m0 +a1*yhat +a2*yres  +&eq +k1*mktrf1 +k2*amihud1 +k3*tover1 +k4*hml1 +k5*smb1 ;
fit ltret / gmm kernel=(bart,13,0) vardef=n; run; quit; 
%print;

%end;

data merged&k; merge final1 - final12 ; run;
data merged&k; set merged&k; fund ="&xvar";

proc append base = Figure1 data = merged&k force; run;

%end;
%mend;

%decomp ;

data Figure1; retain fund type Parameter syy_1 syy_2 syy_3 syy_4 syy_5 syy_6 syy_7 syy_8 syy_9 syy_10 syy_11 syy_12 ;
set Figure1; run;

proc print data = Figure1 noobs; run;
